package com.zjdiepu.www.commpent.helper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.dao.base.BaseAccountBusiUnitMapper;
import com.zjdiepu.www.dao.system.BaseBusiUnitMapper;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.yw.YwRestMapper;
import com.zjdiepu.www.model.base.dto.BaseAccountBusiUnit;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.base.ext.BaseAccountBusiUnitExt;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessBaseRequest;
import com.zjdiepu.www.util.ExceptionHelper;
/**
/**
 * 项目名称：dpqc-parent-manager-information <br/> 
 * 类名称：UserMapSalermanHelper <br/>  
 * 类描述： 系统用户与业务员关联 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年5月12日 上午9:01:41  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年5月12日 上午9:01:41   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@Component
public class UserMapSalermanHelper {
	@Resource
	private BaseSalersmanMapper baseSalersmanMapper;
	@Resource
	private BaseBusiUnitMapper baseBusiUnitMapper;
    @Resource
    private BaseAccountBusiUnitMapper baseAccountBusiUnitMapper;
    @Resource
    private YwBusinessMapper ywBusinessMapper;
    @Resource
    private YwRestMapper ywRestMapper;
    
    /**
     * 根据业务ID查找业务员归属省份编码
     * @author xiongxiaotun <br/>
     * @Title: getSalermanProvinceByBusiId  <br/>
     * @Description: 根根据业务ID查找业务员归属省份编码 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    public String getSalermanProvinceByBusiId(String busiId){
    	if(StringUtils.isBlank(busiId)) {
    		return null;
    	}
    	YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
    	if(null == ywBusiness) {
    		return null;
    	}
    	String salesmanId = ywBusiness.getSalesmanId();
    	return getSalermanProvinceBySalesmanId(salesmanId);
    }
    
    
    /**
     * 根据业务员ID查找业务员归属省份编码
     * @author xiongxiaotun <br/>
     * @Title: getSalermanProvinceBySalesmanId  <br/>
     * @Description: 根据业务员ID查找业务员归属省份编码 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    public String getSalermanProvinceBySalesmanId(String salesmanId){
    	if(StringUtils.isBlank(salesmanId)) {
    		return null;
    	}
    	BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(salesmanId);
    	if(null == baseSalersman) {
    		return null;
    	}
    	String unitId = baseSalersman.getUnitId();
    	return getSalermanProvinceByUnitId(unitId);
    }
    
    /**
     * 根据业务单位ID查找业务员归属省份编码
     * @author xiongxiaotun <br/>
     * @Title: getSalermanProvinceByUnitId  <br/>
     * @Description: 根据业务单位ID查找业务员归属省份编码 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    public String getSalermanProvinceByUnitId(String unitId){
    	if(StringUtils.isBlank(unitId)) {
    		return null;
    	}
    	BaseBusiUnit baseBusiUnit = baseBusiUnitMapper.selectByPrimaryKey(unitId);
    	if(null == baseBusiUnit) {
    		return null;
    	}
    	String provinceId = baseBusiUnit.getProvinceId();
    	if(StringUtils.isBlank(provinceId)) {
    		return null;
    	}
    	provinceId = provinceId.trim();
    	int length = provinceId.length();
    	if(length < 6) {
    		ExceptionHelper.thow("999", "业务单位省份ID[" + provinceId + "]不符合规则");
    	}
    	String provinceCode = provinceId.substring(0, 6);
    	return provinceCode;
    }
    
    
    /**
     * 根据业务ID查找业务员系统账号ID
     * @author xiongxiaotun <br/>
     * @Title: getCustomerIdByBusiId  <br/>
     * @Description: 根据业务ID查找业务员系统账号 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    public Set<String> getSalermanSysUserByBusiId(String busiId){
    	if(StringUtils.isBlank(busiId)) {
    		return null;
    	}
    	YwBusiness ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
    	if(null == ywBusiness) {
    		return null;
    	}
    	String salesmanId = ywBusiness.getSalesmanId();
    	return getSalermanSysUserBySalesmanId(salesmanId);
    }
    
    /**
     * 根据业务员ID查找业务员系统账号ID
     * @author xiongxiaotun <br/>
     * @Title: getSalermanSysUserBySalesmanId  <br/>
     * @Description: 根据业务员ID查找业务员系统账号 <br/>
     * @param busiId
     * @return
     * @version V1.0  <br/>
     */
    public Set<String> getSalermanSysUserBySalesmanId(String salesmanId){
    	if(StringUtils.isBlank(salesmanId)) {
    		return null;
    	}
    	BaseAccountBusiUnitExt baseAccountBusiUnitExt = new BaseAccountBusiUnitExt();
    	baseAccountBusiUnitExt.setBaseWhere();
    	baseAccountBusiUnitExt.withOutPage();
    	baseAccountBusiUnitExt.setIdUnit(salesmanId);
    	List<BaseAccountBusiUnit> baseAccountBusiUnits = baseAccountBusiUnitMapper.selectByExt(baseAccountBusiUnitExt);
    	if(CollectionUtils.isEmpty(baseAccountBusiUnits)) {
    		return null;
    	}
    	return baseAccountBusiUnits.stream().map(item -> item.getUserId()).collect(Collectors.toSet());
    }
	
    /**
     * 获取对应的业务员账号
     * @return
     */
    public BaseSalersman getBySalersmanUserId(String userId){
    	BaseAccountBusiUnitExt baseAccountBusiUnitExt = new BaseAccountBusiUnitExt();
    	baseAccountBusiUnitExt.setBaseWhere();
    	baseAccountBusiUnitExt.setRows(1);
    	baseAccountBusiUnitExt.setUserId(userId);
    	List<BaseAccountBusiUnit> baseAccountBusiUnits = baseAccountBusiUnitMapper.selectByExt(baseAccountBusiUnitExt);
    	if(CollectionUtils.isEmpty(baseAccountBusiUnits)) {
    		return null;
    	}
    	
    	BaseAccountBusiUnit baseAccountBusiUnit = baseAccountBusiUnits.get(0);
    	String salesmanId = baseAccountBusiUnit.getIdUnit(); //业务员ID
    	BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(salesmanId);
    	
    	return baseSalersman;
    }
    
    /**
     * 获取登录用户负责的业务
     * @param currentUser
     * @return
     */
    public List<String> getBusiIdByLoginUser(SysUser currentUser) {
    	YwBusinessBaseRequest ywBusinessBaseRequest = new YwBusinessBaseRequest();
    	ywBusinessBaseRequest.setBaseWhere();
    	ywBusinessBaseRequest.withOutPage();
    	ywBusinessBaseRequest.setSalesmanId(currentUser.getSalersmanId());
		List<YwBusiness> ywBusiness = ywRestMapper.selectByExt(ywBusinessBaseRequest);
		List<String> busiIds = CollectionInitUtil.emptyList();
		if(CollectionUtils.isNotEmpty(ywBusiness)) {
			busiIds = ywBusiness.stream().map(item -> item.getId()).collect(Collectors.toList());
		}
    	return busiIds;
    }
}


















